﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Windows.Forms;
using System.IO;
using System.Reflection;
using System.Text;
using System.Net;
using System.Net.Security;
using System.Security.Cryptography.X509Certificates;
using Library.HttpTools;

namespace iNotesNotification
{
    static class Program
    {
        /// <summary>
        /// The main entry point for the application.
        /// </summary>
        [STAThread]
        static int Main(params string[] args)
        {
            if (args.Length == 0)
            {
                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);
                Application.ThreadException += new System.Threading.ThreadExceptionEventHandler(Application_ThreadException);
                Application.Run(new iNotesForm());
            }
            else
            {
                try
                {
                    string interfaceType = null;
                    bool loginTest = false;
                    string file = null;

                    string current, command = string.Empty;
                    for (int i = 0; i < args.Length; i++)
                    {
                        current = args[i];

                        if (current.StartsWith("-"))
                        {
                            command = current;

                            switch (command)
                            {
                                case "-login":
                                    loginTest = true;
                                    break;
                            }
                        }
                        else
                        {
                            switch (command)
                            {
                                case "":
                                    break;
                                case "-interface":
                                    interfaceType = current;
                                    break;
                                case "-saveas":
                                    file = current;
                                    break;
                            }
                        }
                    }

                    // if the interface is set to xml or html, then fetch the next file based on config
                    iNotesController controller = iNotesController.Instance;
                    if (controller != null)
                    {
                        bool result = false;
                        if (loginTest)
                        {
                            result = controller.IsLoginValid;
                        }
                        else
                        {
                            FileInfo f = new FileInfo(file);
                            result = controller.Download(f);
                        }

                        Console.WriteLine(result.ToString());
                    }
                    else
                    {
                        Console.WriteLine(bool.FalseString);
                    }
                }
                catch (Exception ex)
                {
                    LogException(ex);
                    Console.WriteLine("~~ERROR~~");
                    Console.WriteLine(ex.Message);
                    Console.WriteLine(ex.StackTrace);
                }
            }
            return 0;
        }

        static void Application_ThreadException(object sender, System.Threading.ThreadExceptionEventArgs e)
        {
            LogException(e.Exception);
        }


        public static void LogException(Exception ex)
        {
            try
            {
                DateTime now = DateTime.Now;
                string s = now.ToString("yyyyMMddhhmmssfff");
                s = @"\Errors\" + s + "_" + ex.GetType() + ".txt";

                FileInfo errorFile = new FileInfo(iNotesController.AssemblyLocation.Directory.FullName + s);
                if (!errorFile.Directory.Exists) 
                {
                    Directory.CreateDirectory(errorFile.Directory.FullName);
                }

                StringBuilder sb = new StringBuilder();

                Exception current = ex;
                while (current != null)
                {
                    sb.Append("MESSAGE: ");
                    sb.AppendLine(current.Message);

                    sb.Append("SOURCE: ");
                    sb.AppendLine(current.Source ?? "<NULL>");

                    sb.Append("TARGETSITE: ");
                    if (current.TargetSite != null) sb.AppendLine(current.TargetSite.Name);
                    else sb.AppendLine("<NULL>");

                    sb.Append("STACKTRACE: ");
                    sb.AppendLine(current.StackTrace ?? "<NULL>");

                    if (current.Data != null)
                    {
                        bool b = false;
                        foreach (string key in current.Data.Keys) 
                        {
                            if (b == false) sb.AppendLine("DATA: ");
                            sb.Append(key).Append(" = ").Append(current.Data[key].ToString()).AppendLine();
                            b = true;
                        }
                    }

                    if (current != current.InnerException)
                        current = current.InnerException;
                    else 
                        current = null;
                }

                File.WriteAllText(errorFile.FullName, sb.ToString());
            }
            catch
            {
                // can't do nothin' here
            }
        }
    }
}
